package applications {
	import display.CursorRenderer;

	import jazztures.Jazztures;
	import jazztures.events.CursorEvent;
	import jazztures.events.HoldEvent;
	import jazztures.helpers.CursorHelper;

	import sweatless.graphics.SmartRectangle;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.ui.Mouse;


	public class CursorExample extends Sprite {
		private var kinect:Jazztures;
		private var cursorHelper:CursorHelper;
		private var cursorLayer:Sprite;
		
		public function CursorExample() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			cursorLayer = new Sprite();
			addChild(cursorLayer);
			cursorLayer.mouseEnabled=false;
			cursorLayer.mouseChildren=false;
		
			kinect = new Jazztures();
			kinect.connect();
			cursorHelper = new CursorHelper(stage, kinect);
			kinect.addEventListener(CursorEvent.ADD_CURSOR, cursorListener);
			kinect.addEventListener(CursorEvent.REMOVE_CURSOR, cursorListener);
			kinect.addEventListener(CursorEvent.UPDATE_CURSOR, cursorListener);
			
			addEventListener(Event.ENTER_FRAME, update);
			
			addChild(buttons());
		}
		
		private function buttons(bgColor:uint=0x000000):Sprite{
			var buttonsContainer:Sprite = new Sprite();
			var background:SmartRectangle = new SmartRectangle(stage.stageWidth, stage.stageHeight);
			background.colors = [bgColor];
			buttonsContainer.addChild(background);
			var totalButtons:int = Math.round(Math.random()*10);
			for(var i:int=1; i<totalButtons; i++){
				var button:Sprite = new Sprite();
				var rect:SmartRectangle = new SmartRectangle(100, 100);
				rect.colors = [Math.random()*0xFFFFFF];
				rect.x = Math.random()*(stage.stageWidth - rect.width);
				rect.y = Math.random()*(stage.stageHeight - rect.height);
				button.addChild(rect);
				button.addEventListener(HoldEvent.HOLD_START, holdListener);
				button.addEventListener(HoldEvent.HOLD_RELEASE, holdListener);
				button.addEventListener(HoldEvent.HOLD_PROGRESS, holdListener);
				button.addEventListener(HoldEvent.HOLD_COMPLETE, holdListener);
				buttonsContainer.addChild(button);
			}
			return buttonsContainer;
		}

		private function holdListener(event : HoldEvent) : void {
			switch(event.type){
				case HoldEvent.HOLD_PROGRESS:
					CursorRenderer(cursorLayer.getChildByName("cursor_"+ event.id)).progress =  event.timerProgress;
				break;
				
				case HoldEvent.HOLD_START:
					CursorRenderer(cursorLayer.getChildByName("cursor_"+ event.id)).showProgress();
				break;
				
				case HoldEvent.HOLD_RELEASE:
					CursorRenderer(cursorLayer.getChildByName("cursor_"+ event.id)).hideProgress();
				break;
				
				case HoldEvent.HOLD_COMPLETE:
					CursorRenderer(cursorLayer.getChildByName("cursor_"+ event.id)).hideProgress();
					addChild(buttons(SmartRectangle(event.currentTarget.getChildAt(0)).colors[0]));				
				break;
			}
		}

		private function cursorListener(event : CursorEvent) : void {
			switch(event.type){
				case CursorEvent.ADD_CURSOR:
				case CursorEvent.UPDATE_CURSOR:
					var cursor:CursorRenderer = CursorRenderer(cursorLayer.getChildByName("cursor_" + event.id) || cursorLayer.addChild(new CursorRenderer()));
					cursor.name = "cursor_" + event.id;
					cursor.x = event.x;
					cursor.y = event.y;
					break;
				case CursorEvent.REMOVE_CURSOR:
					if(cursorLayer.getChildByName("cursor_"+event.id)) cursorLayer.removeChild(cursorLayer.getChildByName("cursor_"+event.id));
					break;
			}
		}

		private function update(event : Event) : void {
//			mouseWrap();

			if(getChildIndex(cursorLayer) < numChildren-1) setChildIndex(cursorLayer, numChildren - 1);
		}
		
		private function mouseWrap():void{
			kinect.dispatchEvent(new CursorEvent(CursorEvent.UPDATE_CURSOR, 0, mouseX, mouseY, 0));
			Mouse.hide();
		}
	}
}
